home *** CD-ROM | disk | FTP | other *** search
- Path: atglab.bls.com!Alun.Champion
- From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
- Newsgroups: comp.lang.c++
- Subject: Re: casting virtual base classpointer to derived
- Date: 18 Jan 1996 22:29:29 GMT
- Organization: Computer People Inc.
- Message-ID: <ALUN.CHAMPION.96Jan18172929@g7240065.bridge.bst.bls.com>
- References: <DLE98M.nEI@novice.uwaterloo.ca>
- NNTP-Posting-Host: bstfirewall.bst.bls.com
- In-reply-to: mkalisia@novice.uwaterloo.ca's message of Thu, 18 Jan 1996
- 20:36:22 GMT
-
- In article <DLE98M.nEI@novice.uwaterloo.ca> mkalisia@novice.uwaterloo.ca (Maciej Kalisiak) writes:
-
- : I ran into a "limitation" of C++ that I wasn't aware off, and I was
- : hoping someone out there can tell me how to get around this
- : limitation:
-
- : I have a base class Base. I derive a number of other classes from Base
- : using "virtual public". I have an array/list of Base*, which holds
- : pointers to a whole bunch of object of type Base, or any of the derived
- : ones. I take one of these pointers (Base*) and I know that it was
- : really pointing to a derived class. I try to cast to this derived class
- : but I can't (supposedly all compilers are supposed to issue an error in
- : such a case).
-
- Compilers should not let you assign a Base* to a Derived* but it shouldn't
- stop you explicitly casting it to a Derived*, i.e.:
-
- Base* b = new Derived;
- Derived* d = (Derived*)b;
-
- Though this is considered an unsafe cast.
- The ANSI C++ Standard has introduced RTTI (Run Time Type Information) that
- allows you to safely down cast to derived pointers, though few compilers
- have actually implemented RTTI yet:
-
- Base* b = new Derived;
- Derived* d = dynamic_cast<Derived*>b;
- if (!d)
- deal with error;
-
-
- : Any ideas of how to keep such a list of derived type objets using
- : Base*'s, or how to get around this ???
-
- Is it absolutely necessary to actually be talking to Derived objects ?
- Providing relevent virtual functions in the Base class may be an alternative
- solution.
-
- Regards
-
- -A.
-
- --
- | A.Champion |
-